package com.antler.smt.portal.controller;

import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.antler.smt.entity.ProductionLogEntity;
import com.antler.smt.enums.ResultCodeEnum;
import com.antler.smt.portal.annotation.OperateCut;
import com.antler.smt.portal.mapper.RequirementInfoMapper;
import com.antler.smt.request.portal.OrderLogisticsReq;
import com.antler.smt.request.portal.ProductionLogReq;
import com.antler.smt.request.portal.SupplierPersonReq;
import com.antler.smt.response.admin.GetOrderListResp;
import com.antler.smt.response.admin.GetSupplierListResp;
import com.antler.smt.response.base.Result;
import com.antler.smt.response.portal.GetSmsInfoResp;
import com.antler.smt.service.portal.*;
import com.antler.smt.utils.LogUtil;
import com.antler.smt.utils.SmsUtil;
import com.antler.smt.validate.DataValidator;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;


/**
 * 服务商个人中心
 *
 * @author zhangyi
 * @date 2018/3/30 13:40
 */
@RestController
@RequestMapping("/supplierCenter")
@Api(description = "服务商个人中心")
public class SupplierCenterController extends BaseController{

    /**
     * 日志
     */
    private static Logger log = LogUtil.get();

    @Resource
    private SupplierService supplierService;


    @Resource
    private OrderService orderService;

    @Resource
    private RequirementService requirementService;

    @Resource
    private RequirementInfoMapper requirementInfoMapper;

    @Resource
    private ProductionLogService productionLogService;

    @Resource
    private SupplierShowTimeService supplierShowTimeService;

    /**
     * 获取当前的服务商信息
     *
     * @return
     */
    @PostMapping("/getSupplierInfo")
    @ApiOperation(value = "获取当前的服务商信息", httpMethod = "POST")
    public Result<GetSupplierListResp> getSupplierInfo(){
        Result result = null;
           result = supplierService.getSupplier(currentMember.getId());
        return result;
    }

    /**
     *
     * 获取当前服务商下的所有订单
     *
     * @return
     */
    @PostMapping("/getSupplierOrderList")
    @ApiOperation(value = "获取当前服务商下的所有订单", httpMethod = "POST")
    public Result<GetOrderListResp> getSupplierOrderList(){
        Result result = null;
            result = supplierService.getSupplierOrderList(currentMember.getId());
        return result;
    }

    /**
     *  服务商发货，更新发货方式，更新订单的状态
     *
     * @param orderId 订单id
     * @param request 订单实体
     * @return result
     */
    @PostMapping("/sendGoods/{orderId}")
    @ApiOperation(value = "服务商发货，更新发货方式，更新订单的状态", httpMethod = "POST")
    @OperateCut(operationContent = "服务商发货")
    public Result<ResultCodeEnum> SendGoods(@PathVariable Long orderId, @RequestBody OrderLogisticsReq request){

        DataValidator.isBlank(String.valueOf(orderId),"订单id不能为空！");
        DataValidator.isNull(request,"订单物流实体不能为空！");
        DataValidator.isBlank(String.valueOf(request.getPostCompanyId()),"快递公司id不能为空！");
        DataValidator.isBlank(request.getPostCompanyName(),"快递公司名称不能为空！");
        DataValidator.isBlank(request.getTrackingNo(),"运单号不能为空！");

        Result result = null;
            result = supplierService.sendGoods(orderId , request);
        return result;
    }

    /**
     *
     * 编辑当前服务商个人信息
     *
     * @return
     */
    @PostMapping("/editMemberSupplierInfo")
    @ApiOperation(value = "编辑当前服务商个人信息", httpMethod = "POST")
    @OperateCut(operationContent = "服务商编辑个人信息")
    public Result<ResultCodeEnum> editMemberSupplierInfo(@RequestBody SupplierPersonReq supplierPersonReq){

        DataValidator.isNull(supplierPersonReq,"请求服务商实体不能为空！");
        Result result = null;

        result = supplierService.editMemberSupplierInfo(supplierPersonReq,currentMember.getId());
        return result;
    }

    /**
     * 分类获取服务商的订单  已完成 未完成 未通过
     *
     * @param status
     * @return
     */
    @PostMapping("/getSupplierClassifyInfo/{status}")
    @ApiOperation(value = "分类获取服务商的订单(status:8已完成 非8是未完成[去除了开始3个状态0,1,2])", httpMethod = "POST")
    public Result getSupplierClassifyInfo(@PathVariable Integer status){

        DataValidator.isBlank(String.valueOf(status),"审核状态不能为空！");
        Result result = null;

            result = orderService.getSupplierOrderClassifyInfo(currentMember.getId(),status);
        return result;
    }

    /**
     * 查看需求详情
     *
     * @param requirementId 需求id
     * @return
     */
    @PostMapping("/getSupplierOrderInfo/{requirementId}")
    @ApiOperation(value = "查看需求详情", httpMethod = "POST")
    @OperateCut(operationContent = "服务商查看需求详情")
    public Result getSupplierOrderInfo(@PathVariable Long requirementId){

        DataValidator.isBlank(String.valueOf(requirementId),"需求id不能为空！");
        Result result = null;
            result = requirementService.getRequirementInfo(requirementId);
        return result;
    }

    /**
     * 获取订单详情页面内的检验报告
     *
     * @return
     */
    @GetMapping("/getOrderReport")
    @ApiOperation(value = "获取订单详情页面内的检验报告", httpMethod = "GET")
    @OperateCut(operationContent = "服务商获取订单的检验检疫报告")
    public Result<String> getOrderReport(@RequestParam(value = "orderId") Long orderId){

        DataValidator.isBlank(String.valueOf(orderId),"订单id不能为空！");
        Result result = null;
            result = orderService.getOrderFileInfo(orderId);
        return result;
    }

    /**
     * 服务商完成生产，发送消息给项目专员
     *
     * @param requirmentId
     * @return
     */
    @GetMapping("/productionFinish")
    @ApiOperation(value = "服务商完成生产，发送消息给项目专员", httpMethod = "GET")
    @OperateCut(operationContent = "服务商生产完成")
    public Result<String> productionFinish(@RequestParam(value = "requirementId") Long requirmentId){

        DataValidator.isBlank(String.valueOf(requirmentId),"发布需求id不能为空！");
        Result result = null;
            GetSmsInfoResp smsContent = requirementInfoMapper.getSmsContent(requirmentId);
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
            String format = simpleDateFormat.format(smsContent.getSendDate());
            if(!format.equals(null) && !smsContent.getRealName().equals(null) && !smsContent.getCompanyName().equals(null)){
                SendSmsResponse sendSmsResponse = SmsUtil.sendPickUpSms(smsContent.getMobile(),smsContent.getRealName(),format,smsContent.getCompanyName());
                if(sendSmsResponse.getCode().equalsIgnoreCase("OK") && sendSmsResponse.getMessage().equalsIgnoreCase("OK")){
                    result = new Result(ResultCodeEnum.OPERATION_SUCCESS);
                }else{
                    result = new Result(ResultCodeEnum.APPEND_DATA_ERROR);
                }
            }else{
                result = new Result(ResultCodeEnum.APPEND_DATA_ERROR,"消息发送失败，加工厂名可能为空！");
            }
        return result;
    }


    /**
     * 写入生产日志
     *
     * @return
     */
    @PostMapping("/productionLog")
    @ApiOperation(value = "写入生产日志", httpMethod = "POST")
    @OperateCut(operationContent = "服务商写入生产日志")
    public Result<String> productionLog(ProductionLogReq req){

        DataValidator.isNull(req,"日志实体不能为空！");
        DataValidator.isBlank(String.valueOf(req.getOrderId()),"订单id不能为空！");
        DataValidator.isBlank(req.getLogContent(),"内容不能为空！");
        Result result = null;
            result = productionLogService.writeLog(req);
        return result;
    }

    /**
     * 获取该订单下的生产进度日志
     *
     * @return
     */
    @GetMapping("/getLogByCurrentOrder")
    @ApiOperation(value = "获取该订单下的生产进度日志", httpMethod = "GET")
    @OperateCut(operationContent = "服务商查看生产进度日志")
    public Result<PageInfo<ProductionLogEntity>> getLogByCurrentOrder(@RequestParam(value = "orderId") Long orderId){

        DataValidator.isBlank(String.valueOf(orderId),"订单id不能为空！");
        Result result = null;
            result = productionLogService.getLogByCurrentOrder(orderId,currentMember.getId());
        return result;
    }

    /**
     * 返回此时订单的预定时间或者实际交货时间（type:1预定时间2:实际交货时间）
     *
     * @param orderId 订单id
     * @return
     */
    @GetMapping("/getSupplierTime")
    @ApiOperation(value = "返回此时订单的预定时间或者实际交货时间（type:1预定时间2:实际交货时间）", httpMethod = "GET")
    public Result getSupplierTime(@RequestParam(value = "orderId") Long orderId){

        DataValidator.isBlank(String.valueOf(orderId),"订单id不能为空！");
        Result result = null;
            result = supplierShowTimeService.getSupplierShowTime(orderId);
        return result;
    }
}

